<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";
require "submenu.php";


$campaign = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where ID='$cID'"));

?>


<table width="100%">
<tr>
	<td>
		<p class="small" align="left">Campaigns > <?=$campaign[2]?> > Modify Actions</p>
	</td>
	<td>
		<?load_submenu($cID,$campaign[2])?>
	</td>
</tr>
</table>


<p class="heading">
Actions for <?=ucfirst($campaign[2])?>
</p>

<p>
View actions generated for this campaign, as well as edit or add actions.
</p>

<?

if($submit_action == "Delete Selected")
{
	if (!$checked_action)
		alert("no actions were selected");
	for ($i=0;$i<count($checked_action);$i++)
		mysql_query("delete from adtrackz_actions where ID='$checked_action[$i]'");
	alert("The selected actions have been deleted.");
}
elseif ($submit_action == "Save Changes")
{
	for ($i=0;$i<count($ID);$i++) //update all values on screen
	{
		mysql_query("update adtrackz_actions set subID='$sID[$i]', inputID='$userID[$i]', name='$name[$i]', description='$description[$i]' where ID='$ID[$i]'");
	}
	alert("Actions have been modified.");
}

//break it down into pages
if($from && !preg_match("/\b\d+\b/",$from))
{
	error("Starting from action # must be an integer value.");
	$from = 0;
}
elseif (!$from)
	$from = 0;
else
	$from -= 1;
if($display_limit && !preg_match("/\b\d+\b/",$display_limit))
{
	error("Number of rows must be an integer value.");	
	$display_limit = 20;
}
if (!$display_limit)
	$display_limit = 20;

$actions = mysql_query("select * from adtrackz_actions where campaignID='$cID' order by timest limit $from, $display_limit");
$from += 1;
?>

<form action="dstats-actions-modify.php" method="post">
<input type="hidden" name=cID value=<?=$cID?>>
<center>
<table width=90%>
<tr>
	<td>
		<input type="submit" name="submit_display" value="Show:"  >
		<input type="text" name=display_limit  size=3 maxlength=3 value=<?=$display_limit?>> rows 
		starting from action #<input type="text" name=from  size=3 maxlength=10 value=<?=$from?>>
	</td>
	<td align="right">
		<a href="dstats-actions-add.php?cID=<?=$cID?>">Add More Actions...</a><br>
		<a href="dstats-actions.php?cID=<?=$cID?>">View Actions...</a>
	</td>
</tr>
</table>
<table width=90%>
<tr>
	<td>
	<?
	$total_actions = mysql_num_rows(mysql_query("select * from adtrackz_actions where campaignID='$cID'"));
	//break it down into pages
	$pages = $total_actions / $display_limit;
	$remainder = $total_actions % $display_limit;
	for($i=1;$i<=$pages;$i++)
	{
		if ($i == 1)
		{
			$page_start = $i;
			$page_end = $i*$display_limit;	
			echo "<a href=dstats-actions-modify.php?from=$page_start&display_limit=$display_limit&cID=$cID>$page_start - $page_end</a> | ";	
		}
		elseif ($i < $pages)
		{
			$page_start = (($i*$display_limit) - $display_limit) + 1;
			$page_end = $i*$display_limit;	
			echo "<a href=dstats-actions-modify.php?from=$page_start&display_limit=$display_limit&cID=$cID>$page_start - $page_end</a> | ";	
		}
		elseif ($i == $pages && !$remainder)
		{
			$page_start = (($i*$display_limit) - $display_limit) + 1;
			$page_end = $i*$display_limit;	
			echo "<a href=dstats-actions-modify.php?from=$page_start&display_limit=$display_limit&cID=$cID>$page_start - $page_end</a>";	
		}
	}
	if($remainder)
	{
		$page_start = $page_end + 1;
		$page_end = $page_end + $remainder;
		echo "<a href=dstats-actions-modify.php?from=$page_start&display_limit=$display_limit&cID=$cID>$page_start - $page_end</a>";		
	}
	?>		
	</td>
</tr>
</table>
<br><br>

<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">Number</td>
	<td class="tbhead1">ID</td>
	<td class="tbhead1">Date</td>
	<td class="tbhead1">Subcampaign</td>
	<td class="tbhead1">Name</td>
	<td class="tbhead1">Description</td>
	<td class="tbhead1">Action</td>
</tr>
<?
if (!mysql_num_rows($actions))
	echo "<tr><td colspan=7>There are no actions to display.</td></tr></table>";
else
{
	//get all the subcampaigns to display in a pulldown list
	//do it up here first so we only query the database once (build array)
	$allsubdata = mysql_query("select * from adtrackz_subcampaigns where campaignID='$cID' order by adcode asc");	
	while ($tempsub = mysql_fetch_row($allsubdata))
		$allsubs[$tempsub[0]] = $tempsub[2];

	//loop through all the actions
	for($i=0;$i<mysql_num_rows($actions);$i++)
	{
		$action = mysql_fetch_row($actions);
		//convert timestamp from database to human readable
		$timest = date("D M j G:i:s T Y", $action[3]);
		$subadcodedata = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$action[2]'"));
		$action_subadcode = $subadcodedata[0];
print <<<END
<tr>
	<td class="tb1"><input type=hidden name=ID[] value=$action[0]>$action[0]</td>
	<td class="tb1"><input type=input name=userID[] size=4 value="$action[4]"></td>
	<td class="tb1">$timest</td>
	<td class="tb1">
	<select name="sID[]">
END;
		//selected for the sub for this action
		if (!$action_subadcode)
			echo "<option value=\"\" selected>None</option>";
		else
			echo "<option value=\"\">None</option>";	
		foreach ($allsubs as $subID => $subadcode)
		{
			if ($action_subadcode == $subadcode)
				echo "<option value=\"$subID\" selected>$subadcode</option>";
			else
				echo "<option value=\"$subID\">$subadcode</option>";
		}
print <<<END
	</select></td>
	<td class="tb1"><input type=input name=name[] size=8 value="$action[5]"</td>
	<td class="tb1"><input type=input name=description[] value="$action[6]"</td>
	<td class="tb1"><input type=checkbox name=checked_action[] value="$action[0]"></td>
</tr>
END;
	}
}
?>

</table>
<br><br>
<input type=submit name=submit_action value="Save Changes">
&nbsp;&nbsp;
<input type=submit name=submit_action value="Delete Selected">
</center>
</form>
<br>


<?
require "footer.php";

?>